package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    //分页查询
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //分页查询套餐信息
        //分页条件
        Page<Setmeal> page = new Page<>(pageNum,pageSize);

        //业务条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNoneEmpty(name),Setmeal::getName,name);
        //执行查询
        page = setmealMapper.selectPage(page,wrapper);

        //遍历列表获得每个套餐
        if (CollectionUtil.isNotEmpty(page.getRecords())){
            for (Setmeal setmeal : page.getRecords()) {
                //根据套餐的category_id去category表中获取分类名称
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
                //根据套餐id去setmeal_dish中查询套餐信息
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishList);

            }
        }

        return page;
    }
    //查询菜品列表
    @Override
    public List<Dish> findList(Long categoryId,String name) {
        //条件封装
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getStatus,1)
                .eq(categoryId != null,Dish::getCategoryId,categoryId)
                .like(StringUtils.isNoneEmpty(name),Dish::getName,name);
        //执行查询

        return dishMapper.selectList(wrapper);
    }

    //添加
    @Override
    public void save(Setmeal setmeal) {
        //保存基本信息到Setmeal表
        setmealMapper.insert(setmeal);
        //获取菜品列表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)){
            int i = 0;
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDish.setSort(i++);
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    //批量删除
    @Override
    public void deleteByIds(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getStatus,1)
                .in(Setmeal::getId,ids);
        Integer num = setmealMapper.selectCount(wrapper);
        if (num > 0){
            throw new CustomException("有套餐正处于售卖状态,不允许删除");
        }
        //删除setmeal_dish表中setmeal_id
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper1);
        //删除id在ids中
        setmealMapper.deleteBatchIds(ids);
    }

    //根据id查询套餐
    @Override
    public Setmeal findById(Long id) {
        //从套餐表根据id查询信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //根据套餐的category_id去category表中获取分类名称
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());

        //根据套餐id去setmeal_dish查询套餐信息
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
        setmeal.setSetmealDishes(setmealDishList);

        return setmeal;
    }

    //修改
    @Override
    public void update(Setmeal setmeal) {
        setmealMapper.updateById(setmeal);
        //根据setmeal_id删除setmeal_dish中得相关信息(条件删除)
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(wrapper);
        //重新添加
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)){
            int i = 0;
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDish.setSort(i++);
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);
        return setmealMapper.selectList(wrapper);
    }

    @Override
    public List<Dish> findDishList(Long id) {
        //根据套餐id查询出当前套餐下的菜品集合
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);

        //遍历集合
        ArrayList<Dish> dishList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(setmealDishList)){
            for (SetmealDish setmealDish : setmealDishList) {
                Dish dish = dishMapper.selectById(setmealDish.getDishId());
                dish.setCopies(setmealDish.getCopies());
                dishList.add(dish);
            }
        }
        return dishList;

    }
}
